<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>Comet echo RPC</title>

    <script type="text/javascript" src="${pageContext.request.contextPath}/js/dojo/dojo.js.uncompressed.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/dojox/cometd.js.uncompressed.js"></script>

    <script type="text/javascript" src="${pageContext.request.contextPath}/js/prototype.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/behaviour.js"></script>

    <script language="JavaScript" type="text/javascript">

        dojo.require("dojox.cometd");
        dojo.require("dojox.cometd.timestamp");        

        $ = dojo.byId;
            
        var echoBehaviours = {
            '#phrase': {
                "found": function(e){
                    e.setAttribute("autocomplete","OFF");
                },
                "onkeyup": function(e){
                    if(e.keyCode == dojo.keys.ENTER){
                        echoRpc($('phrase').value);
                        $('phrase').value='';
                        return false;
                    }
                    return true;
                }
            },

            '#sendB': {
                "onclick": function(e){
                    echoRpc($('phrase').value);
                    $('phrase').value='';
                    return false;
                }
            }
        };

        function setUp() {
            var element=dojo.byId('phrase');
            element.setAttribute("autocomplete","OFF");
            dojo.connect(element, "onkeyup", function(e){
                      if(e.keyCode == dojo.keys.ENTER){
                          echoRpc($('phrase').value);
                          $('phrase').value='';
                          return false;
                      }
                      return true;
                  });              element=dojo.byId('sendB');
            dojo.connect(element, "onclick", function(e){
                      echoRpc($('phrase').value);
                      $('phrase').value='';
                      return false;
                  });
            
            dojox.cometd.init("${pageContext.request.contextPath}/cometd");
            dojox.cometd.subscribe("/service/hello", echoRpcReturn);        
        }

        function echoRpc(msg) {
            console.debug(msg);
            dojox.cometd.publish("/service/hello", msg);
        }
        
        function echoRpcReturn(msg) {
            dojo.byId("responses").innerHTML += (msg.timestamp?msg.timestamp:"")+" "+msg.channel+": "+msg.data+"\n";
        }

        dojo.addOnLoad(setUp);
    </script>
</head>
<body>

<h1>Hello Test (Spring Enabled)</h1>

<p>This example shows that BayeuxMessageHandler are first class Spring beans that can be configured in the usual way. In
    this
    case the HelloMessageHandler is injected with a reference to the HelloService bean which is responsible for
    generating the responses.</p>

<div>Your name:&nbsp;<input id="phrase" type="text"></input> <input id="sendB" class="button" type="submit" name="join"
                                                                    value="Send"/>
</div>
<pre id="responses"></pre>

<h3>Java Code</h3>

    <pre>
        public class HelloMessageHandler implements BayeuxMessageHandler
        {
            private HelloService helloService;

            @Required
            public void setHelloService(HelloService helloService) {
                this.helloService = helloService;
            }

            public void handle(Bayeux bayeux, Channel channel, Client client, Object data) {
                Channel replyChannel = bayeux.getChannel(channel.getId() + "/" + client.getId());
                if (replyChannel != null) {
                    replyChannel.publish(helloService.sayHello(data.toString()), client);
                }
            }
        }
    </pre>

<h3>Spring Configuration</h3>

    <pre>
        &lt;bean id="helloService" class="comet.helloworld.spring.HelloServiceImpl"/>

        &lt;bean id="helloDataFilter" class="comet.helloworld.spring.BayeuxDataFilterContainer">
            &lt;property name="clientName" value="helloServiceClient"/>
            &lt;property name="channelName" value="/helloService"/>
            &lt;property name="filter">
                &lt;bean class="comet.helloworld.spring.HelloServiceDataFilter">
                    &lt;property name="helloService" ref="helloService"/>
                &lt;/bean>
            &lt;/property>
        &lt;/bean>
    </pre>

</body>
</html>
